# -*- coding: utf-8 -*-
# @Author: 许
# @Date: 2019-12-31 15:57:55
# @LastEditTime: 2020-03-01 18:44:12
# @Description: 识别插件。
# 在runtime/photo/增加一个image文件夹保存图片
import os,re
from MsgProcess import MsgProcess, MsgType
from api.BDaip import AipImageClassify, AipOcr
import cv2 as cv

        
class Recognition(MsgProcess):
    
    def Text(self, message):
        Data = message['Data']
        
        APP_ID = '改自己的'
        API_KEY = '改自己'
        SECRET_KEY = '改自己的'

        cap =cv.VideoCapture(0)
        isOpened = cap.isOpened()  ##判断视频是否打开
        print(isOpened)
        fps = cap.get(cv.CAP_PROP_FPS)  ##获取帧率
        width = int(cap.get(cv.CAP_PROP_FRAME_WIDTH))   ###获取宽度
        height = int(cap.get(cv.CAP_PROP_FRAME_HEIGHT))   ###获取高度
        print(fps,width,height)
        i=0
        while isOpened :
             if i ==1:   ###只保存前一张
                 break
             else:
                 i= i+1
             (flag,frame)=cap.read()
             fileName = "image"+str(i)+".jpg"
             print(fileName)
             if flag == True :
                 cv.imwrite("runtime/photo/image/"+str(i)+".jpg",frame,[cv.IMWRITE_JPEG_CHROMA_QUALITY,100])  ##命名 图片 图片质量
             print("end!")        
        
        Triggers = ["什么字", "这里有什么字", "这个什么字", "那里有什么字"]
        if any(map(lambda trig: trig in Data, Triggers)):        
                
                client = AipOcr(APP_ID, API_KEY, SECRET_KEY)

                """ 读取图片 的方法"""
                def get_file_content(filePath):
                    with open(filePath, 'rb') as fp:
                        return fp.read()

                #定义图片的绝对路径（在runtime/photo/增加一个image文件夹）
                image = get_file_content('runtime/photo/image/1.jpg')

                """ 调用通用文字识别（高精度版） """
                restu1 = client.basicAccurate(image);

                lists = restu1['words_result']      #列表
                for listss in lists:
                    self.say(listss['words'])
                    
        Triggers = ["什么花", "这里有什么花", "这个什么花", "那里有什么花"]
        if any(map(lambda trig: trig in Data, Triggers)):        
                
                client=AipImageClassify(APP_ID,API_KEY,SECRET_KEY)

                def get_txt(image_con):
                    res=client.plantDetect(image_con)
                    return res
                def get_file_content(filePath):
                    with open("runtime/photo/image/1.jpg",'rb') as fp:
                        return fp.read()
                for i in range(1):
                    img_name='[].jpg'.format(i)
                    image=get_file_content(img_name)
                    ret=get_txt(image)
                    self.say(ret["result"][0]["name"])

        Triggers = ["什么车", "这里有什么车", "这个什么车", "那里有什么车"]
        if any(map(lambda trig: trig in Data, Triggers)):

                client = AipImageClassify(APP_ID, API_KEY, SECRET_KEY)

                # 打开图片文件并读取二进制图片信息
                def get_file_content(file_path):
                    with open(file_path, 'rb') as f:
                        return f.read()

                image = get_file_content("runtime/photo/image/1.jpg")

                """
                    调用车辆识别
                """
                # {"top_num": 1} 表示返回的多个车型中的第一个

                # 调用client对象的carDectect方法
                self.say(client.carDetect(image, options={"top_num": 1})["result"][0]["name"])
                
        Triggers = ["什么菜", "这里有什么菜", "这个什么菜", "那里有什么菜"]
        if any(map(lambda trig: trig in Data, Triggers)):
        
                client = AipImageClassify(APP_ID, API_KEY, SECRET_KEY)

                """ 读取图片 """
                def get_file_content(filePath):
                    with open(filePath, 'rb') as fp:
                        return fp.read()

                image = get_file_content("runtime/photo/image/1.jpg")

                """ 调用菜品识别 """
                result = client.dishDetect(image)

                self.say(result["result"][0]["name"]) 

        Triggers = ["什么东西", "这里有什么东西", "这个什么东西", "那里有什么东西"]
        if any(map(lambda trig: trig in Data, Triggers)):
        
                client = AipImageClassify(APP_ID, API_KEY, SECRET_KEY)
                 
                """ 读取图片 """
                def get_file_content(filePath):
                    with open(filePath, 'rb') as fp:
                        return fp.read()
                 
                image = get_file_content(r"runtime/photo/image/1.jpg")
                #注意文件地址的\与/
                 
                """ 调用通用物体识别 """
                result = client.advancedGeneral(image)
                """ 读取识别相似度最高的结果 """
                self.say(result["result"][0]['keyword'])
       
        self.Stop() # 说完即退出
